package com.xxl.job.admin.api.utils;

import cn.hutool.crypto.SecureUtil;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;


/**
 * 签名加密
 */
public class AppSignUtil {
    /**
     * 获取签名
     * @param times 时间戳
     * @return
     */
    public static String sign(String appSecret,String times){
        try{
            Mac mac = Mac.getInstance("HmacSHA256");
            SecretKeySpec key = new SecretKeySpec(appSecret.getBytes(), "HmacSHA256");
            mac.init(key);
            return SecureUtil.md5(Arrays.toString(mac.doFinal(times.getBytes())));
        }catch (NoSuchAlgorithmException | InvalidKeyException e){
            throw new RuntimeException("sign api secret failed", e);
        }
    }

}
